Skip to content

fix(rpc): don't panic when printing out-of-range timestamp or since values - #689

Merged
chenyukang merged 1 commit into
nervosnetwork:developfrom
Officeyutong:fix/rpc-out-of-range-timestamp
Nov 9, 2026
Merged

chenyukang merged 1 commit into
nervosnetwork:developfrom
Officeyutong:fix/rpc-out-of-range-timestamp

Conversation

@Officeyutong

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

timestamp_to_string and since_to_string in src/utils/rpc/primitive.rs format values from RPC responses with unwrap() on fallible conversions:

  • Local.timestamp_millis_opt(..).unwrap() for header, sync state and banned address timestamps;
  • Since::extract_metric().unwrap() and Local.timestamp_opt(..).unwrap() for input since values.

A timestamp outside the range chrono can represent, or a since with an invalid metric flag, makes ckb-cli panic while printing the response. Honest nodes do not return such values, but an RPC endpoint the user connects to can, and the CLI exits instead of showing the result.

What is changed and how it works?

  • timestamp_to_string prints <value> (invalid timestamp) when the value cannot be converted to a local time.
  • since_to_string prints 0x<value> (invalid since) for an unknown metric flag, and timestamp(<value>, invalid) for a timestamp since that cannot be converted.
  • Valid values are printed as before.

Check List

Tests

  • Unit tests test_out_of_range_timestamp_does_not_panic and test_invalid_since_does_not_panic in src/utils/rpc/primitive.rs.
  • cargo clippy --all-targets -- -D warnings passes.

Side effects

  • None.

Release note

Fix: ckb-cli no longer panics when an RPC response contains an out-of-range timestamp or an invalid since value.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🟢 Approval recommended

The change is narrowly focused on making RPC timestamp/since formatting more robust by replacing panicking conversions with fallible handling and adding targeted unit tests, with no active issues indicated.

No open issues 🎉

What changed in this PR

This pull request hardens RPC primitive formatting in ckb-cli to avoid panics when an RPC endpoint returns malformed or out-of-range timestamp / since values, ensuring the CLI can still print responses safely.

Changes:

  • Replace unwrap()-based timestamp conversions with fallible handling and stable “invalid timestamp” output.
  • Gracefully handle invalid since metric flags and unrepresentable timestamp-since values with explicit “invalid since” formatting.
  • Add unit tests to ensure these cases do not panic and valid values still render as expected.
File Summary
src/​utils/​rpc/​primitive.rs Makes timestamp/since string formatting resilient to out-of-range/invalid RPC-provided values and adds regression tests.

Review effort: Balanced

@chenyukang
chenyukang merged commit ccca1c9 into nervosnetwork:develop Nov 9, 2026
8 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants